<template>
  <div style="">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <p class="bg-info text-center" style="padding: 8px;">过户信息</p>
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-4 form-group form-input-group" :class="[$v.f_newuser_name.required ? 'has-error' : 'has-success']">
            <label for="f_newuser_name" style="width: 100px" class="control-label" >新用户姓名</label>
            <input class="form-control" type="text" title="新用户姓名" v-model="model.f_newuser_name"
             v-validate:f_newuser_name='{required: true }' placeholder="新用户姓名" v-next-el='phone'>
             <!-- <label v-if="$v.f_newuser_name.required && namespan" class = "" style = "color:red">不能为空</label> -->
          </div>

          <div class="col-sm-8 form-group form-input-group" :class="[$v.f_newidnumber.identityCardValid ? 'has-error' : 'has-success']">
            <label for="f_newidnumber" class=" control-label" >新身份证号</label>
              <input class="form-control" type="text" title="新身份证号" v-model="model.f_newidnumber"
               v-validate:f_newidnumber='{identityCardValid: true }'  placeholder="新身份证号"  v-next-el='fees' v-el:number>
            <!-- <label v-if="$v.f_newidnumber.identityCardValid && numspan" class = "control-label " style = "color:red;line-height:100%">请输入正确的身份证号</label> -->
          </div>
        </div>
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-4 form-group form-input-group" :class="[$v.f_newuser_phone.required ? 'has-error' : 'has-success']">
            <label for="f_newuser_phone" class=" control-label" >新电话</label>
              <input title="新电话"  class="form-control" type="text" v-model="model.f_newuser_phone"
               v-validate:f_newuser_phone='{required: true }'
               placeholder="新电话"  v-next-el='number' v-el:phone>
            <!-- <label v-if="$v.f_newuser_phone.required && phonespan" class = "control-label" style = "color:red">不能为空</label> -->
          </div>

          <div class="col-sm-4 form-group form-input-group">
            <label for="f_paymenttype" class="control-label">缴费类型</label>
              <v-select :value.sync="model.f_paymenttype"
                  v-model="model.f_paymenttype"
                  :options='costtypes' placeholder='缴费类型'
                  @change='changecost'
                  close-on-select></v-select>
          </div>

          <div class="col-sm-4 form-group form-input-group">
            <label for="f_transfer_fees" class="control-label">过户费</label>
              <input class="form-control" type="number"  v-model="model.f_transfer_fees"  placeholder="过户费"
                 v-next-el='accopen' v-el:fees v-if="bankinfo">
              <input class="form-control" type="number"  v-model="model.f_transfer_fees"  placeholder="过户费"
                v-el:fees v-else>
        </div>

          <!-- <label for="f_chargename" class=" col-sm-1 control-label">付费方姓名:</label>
          <div class="col-sm-2">
            <input class="form-control" type="text" v-model="model.f_chargename" placeholder="付费方姓名">
          </div> -->
        </div>
        <div class="row" style="margin-top:10px;" v-if = "bankinfo">
          <div class="col-sm-4 form-group form-input-group">
            <label for="f_newbank_accopen" class=" control-label">新银行开户名:</label>
            <input class="form-control" type="text" title="新银行开户名" v-model="model.f_newbank_accopen"
             placeholder="新银行开户名" v-next-el='newbank' v-el:accopen>
          </div>
          <div class="col-sm-8 form-group form-input-group" :class="[$v.f_bank_idnumber.identityCardValid ? 'has-error' : 'has-success']">
            <label for="f_bank_idnumber" class=" control-label" >新开户身份证号</label>
              <input class="form-control" type="text" title="新开户身份证号" v-model="model.f_bank_idnumber"
               v-validate:f_bank_idnumber='{identityCardValid: true }'  placeholder="新开户身份证号">
            <!-- <label v-if="$v.f_newidnumber.identityCardValid && numspan" class = "control-label " style = "color:red;line-height:100%">请输入正确的身份证号</label> -->
          </div>

        </div>
        <div class="row" style="margin-top:10px;" v-if = "bankinfo">
          <div class="col-sm-4 form-group form-input-group">
          <label for="f_newbank_name" class="control-label">新银行名:</label>
             <v-select :value.sync="model.f_newbank_name"
                 :options='banknames' placeholder='新银行开户名'
                 close-on-select></v-select>
          </div>
          <div class="col-sm-8 form-group form-input-group">
          <label for="f_newbank_account" class="control-label">新银行账号:</label>
            <input class="form-control" type="text" title="新银行账号" v-model="model.f_newbank_account"
            placeholder="新银行账号" v-el:account>
          </div>
        </div>
        <div class="row" style="margin-top:10px;">
          <div class="col-sm-12 form-group form-input-group">
            <label for="f_note" class=" control-label">备注:</label>
              <textarea class="form-control" title="备注" v-model="model.f_note"
               rows="3" placeholder="备注"></textarea>
          </div>
        </div>
      </form>
      <div style="text-align:right;height:auto;margin-top:40px;margin-right:50px;">
        <button class="btn btn-success" @click="confirm()"  :disabled='!$v.valid' id='comprehen-comOpe-chaAcc-traMan-确认'>
          确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认
        </button>
        <button class="btn btn-default" @click="clear()" style="width:80px;margin-left:20px;" id='comprehen-comOpe-chaAcc-traMan-取消'>
          取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消
        </button>
      </div>
    </validator>
  </div>
</template>
<script>
import AppData from '../../../../../stores/AppData'
import * as Util from '../../../../Util'
import co from 'co'
let transfersave = function * (self, condition) {
  self.$info('正在添加银行信息...')
  yield self.$SqlService.singleTable('t_infobank', condition).then((bank) => {
    self.setbank(bank)
    self.$info(`数据结构整理完成： transferdata: ${JSON.stringify(self.transferdata)}`)
  }).catch((error) => {
    if (error.status) {
      self.$warn(`查询银行数据出错, ${JSON.stringify(error)}`)
    } else {
      throw error
    }
  })
  yield self.$LogicService.changeAccount(self.transferdata)
}
export default {
  title: '过户管理',
  props: ['row'],
  data () {
    return {
      bankinfo: false,
      numspan: false,
      namespan: false,
      phonespan: false,
      model: {
        f_paymenttype: '现金缴费',
        f_transfer_fees: AppData.getSingleValue('过户费'),
        f_newbank_accopen: '',
        f_newbank_name: ''
      },
      transferdata: {
        userinfo: {}
      }
    }
  },
  watch: {
    'row' (val) {
      this.transferdata = {}
      // this.$info(`接受到数据：${JSON.stringify(this.row)}`)
      // this.transferdata = Object.assign({}, this.transferdata, this.getOperateInfo(), val)
    }
  },
  methods: {
    // 用户更改信息
    getuserinfo () {
      return {
        f_userinfo_id: this.row.f_userinfo_id,
        f_user_name: this.model.f_newuser_name,
        f_idnumber: this.model.f_newidnumber,
        f_user_phone: this.model.f_newuser_phone,
        f_cost_type: this.model.f_paymenttype[0],
        version: this.row.userinfo_version,
        infobank: {
          f_userinfo_id: this.row.f_userinfo_id,
          f_bank_name: this.model.f_newbank_name[0],
          f_bank_accopen: this.model.f_newbank_accopen,
          f_bank_account: this.model.f_newbank_account,
          f_bank_idnumber: this.model.f_bank_idnumber
        }
      }
    },
    changecost (val) {
      console.log('观察到变化了，，，，，银行的')
      if (val[0] === '银行代扣') {
        this.bankinfo = true
      } else {
        this.bankinfo = false
      }
      this.$resetValidation()
    },
    // 过户表及操作表信息
    getOperateInfo () {
      return {
        f_userinfo_id: this.row.f_userinfo_id,
        record_userinfo: this.row.f_userinfo_id,
        f_user_id: this.row.f_user_id,
        f_user_name: this.row.f_user_name,
        f_user_phone: this.row.f_user_phone,
        f_idnumber: this.row.f_idnumber,
        f_newuser_name: this.model.f_newuser_name,
        f_newuser_phone: this.model.f_newuser_phone,
        f_newidnumber: this.model.f_newidnumber,
        f_bank_accopen: null,
        f_bank_name: null,
        f_bank_account: null,
        f_newbank_accopen: this.model.f_newbank_accopen,
        f_newbank_name: this.model.f_newbank_name[0],
        f_newbank_account: this.model.f_newbank_account,
        f_bank_idnumber: this.row.f_bank_idnumber,
        f_newbank_idnumber: this.model.f_bank_idnumber,
        f_transfer_fees: this.model.f_transfer_fees,
        f_note: this.model.f_note,
        f_operat_type: '过户',
        f_describe: Util.f.name + '对用户' + this.row.f_user_name + '进行过户',
        f_state: '有效',
        f_transfer_state: '有效',
        f_operator: Util.f.name,
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname,
        deletebank: false
      }
    },
    // 银行信息的设置
    setbank (bank) {
      if (bank.data[0] == null || bank.data[0] === '') {
        if (this.model.f_paymenttype[0] === '现金缴费') {
          delete this.transferdata.userinfo.infobank
        } else if (this.model.f_paymenttype[0] === '银行代扣') {
          delete this.transferdata.userinfo.infobank.f_userinfo_id
        }
      } else {
        if (this.model.f_paymenttype[0] === '现金缴费') {
          this.transferdata.deletebank = true
          delete this.transferdata.userinfo.infobank
        } else if (this.model.f_paymenttype[0] === '银行代扣') {
          this.transferdata.f_bank_name = bank.data[0].f_bank_name
          this.transferdata.f_bank_accopen = bank.data[0].f_bank_accopen
          this.transferdata.f_bank_account = bank.data[0].f_bank_account
          this.transferdata.f_bank_idnumber = bank.data[0].f_bank_idnumber
        }
      }
    },
    transfer () {
      let condition = 'f_userinfo_id = ' + this.row.f_userinfo_id + ''
      let gen = transfersave(this, condition)
      co(gen).then((res) => {
        this.$dispatch('success', '过户', this.row, res)
      }).catch(
        (error) => this.$dispatch('error', '过户', this.row, error)
      )
    },
    confirm () {
      this.newbankname()
      this.transferdata = Object.assign({}, this.getOperateInfo())
      this.transferdata.userinfo = Object.assign({}, this.getuserinfo())
      console.log('过户保存', this.transferdata)
      this.transfer()
    },
    clear () {
      this.$dispatch('clean', this.row)
    },
    newbankname () {
      if (this.model.f_paymenttype[0] !== '银行代扣') {
        this.model.f_newbank_accopen = null
      }
    }
  },
  computed: {
    banknames () {
      return AppData.getParam('银行名称')
    },
    costtypes () {
      return AppData.getParam('缴费类型')
    }
  }
}
</script>

<style>
</style>
